<template>
  <el-container class="home-container">
    <EnterTx v-if="isEnterTx" />
    <!-- 头部区域 -->
    <el-header>
      <div class="header-main">
        <img src="../assets/img/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '7vh' : '25vh'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#294d4d"
          text-color="#fff"
          active-text-color="#45d1d1"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- :default-active="$route.path" -->
          <!-- 一级菜单 -->
          <!-- index 数值和字符串拼接得到字符串 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- menu开启路由跳转模式 激活导航时 会以index作为path进行路由跳转 -->
            <el-menu-item
              :index="'/' + item2.path"
              v-for="item2 in item.children"
              :key="item2.id"
              @click="saveNavState('/' + item2.path)"
            >
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ item2.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>

        <!-- 友情链接 -->
        <el-link
          class="friends"
          type="info"
          href="http://wooden-joint.gitee.io/pyg/"
          >友情链接</el-link
        >
      </el-aside>
      <!-- 右边主题 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import EnterTx from "./EnterTx.vue";
export default {
  components: {
    EnterTx,
  },
  data() {
    return {
      isEnterTx: true,
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  mounted() {
    setTimeout(() => {
      this.isEnterTx = false;
    }, 1500);
  },
  methods: {
    logout() {
      // 清空里面的token
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取左侧的菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    // 点击按钮 切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #172729;
  display: flex;
  /* 左右贴边布局 */
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 22px;
}
.header-main {
  display: flex;
  align-items: center;
}
.header-main span {
  margin-left: 20px;
}

.el-aside {
  background-color: #273a3d;
}
.el-aside .el-menu {
  border: none;
}

.el-main {
  background-color: #eaedf2;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #397575;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  /* 字间距 */
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-menu-item {
  min-width: 180px !important;
  /* width: 100% !important; */
}

.friends {
  position: fixed;
  bottom: 4vh;
  left: 2vw;
  font-size: 20px;
}
</style>